var crud = require('../../util/crud');
var model = require('../../model/index.js');
var Student = model.get('Student');
var response = require('../../util/response');
var modules = {};

modules.create = (req, res) => {
    Student.create(req.body).then(student => {
    	return res.json(response.success({data: student}))
    }).catch(err => {
        console.log('Student create err, err = ' + err);
        res.json(response.error('新建学生失败')).end();
    })
}
modules.find = (req, res) => {
    const query = req.query,
        filter = query.filter || {},
        limit = query.limit,
        skip = query.skip;

    Student.find(filter).limit(limit).skip(skip).then((students) => {
    	return res.json(response.success({data: students}))
    }).catch(err => {
        console.log('Student find err, err = ' + err);
        return res.json(response.error('查找学生失败')).end();
    })
}
modules.update = (req, res) => {
    const filter = req.query;
    const update = req.body;

    Student.update(filter, update).then((student) => {
    	return res.json(response.success({message: '更新学生成功', data: student}))
    }).catch(err => {
        console.log('Student update err, err = ' + err);
        return res.json(response.error('更新学生失败')).end();
    })
}
modules.remove = (req, res) => {
    const filter = req.query;

    Student.remove(filter).then((result) => {

    	return res.json(response.success({message: '删除学生成功'}))
    }).catch(err => {
        console.log('Student remove err, err = ' + err);
        return res.json(response.error('删除学生失败')).end();
    })
}

module.exports = (router) => {
    crud(router, modules);
}